1 /*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas. Dual MIT/BSD license */
2 /*! NOTE: If you're already including a window.matchMedia polyfill via Modernizr or otherwise, you don't need this part */
3 window.matchMedia = window.matchMedia || (function(doc, undefined){
4
5 var bool,
6 docElem = doc.documentElement,
7 refNode = docElem.firstElementChild || docElem.firstChild,
8 // fakeBody required for <FF4 when executed in <head>
9 fakeBody = doc.createElement('body'),
10 div = doc.createElement('div');
11
12 div.id = 'mq-test-1';
13 div.style.cssText = "position:absolute;top:-100em";
14 fakeBody.style.background = "none";
15 fakeBody.appendChild(div);
16
17 return function(q){
18
19 div.innerHTML = '­<style media="'+q+'"> #mq-test-1 { width: 42px; }</style>';
20
21 docElem.insertBefore(fakeBody, refNode);
22 bool = div.offsetWidth == 42;
23 docElem.removeChild(fakeBody);
24
25 return { matches: bool, media: q };
26 };
27
28 })(document);
29
30
31
32
33 /*! Respond.js v1.2.0: min/max-width media query polyfill. (c) Scott Jehl. MIT/GPLv2 Lic. j.mp/respondjs */
34 (function( win ){
35 //exposed namespace
36 win.respond = {};
37
38 //define update even in native-mq-supporting browsers, to avoid errors
39 respond.update = function(){};
40
41 //expose media query support flag for external use
42 respond.mediaQueriesSupported = win.matchMedia && win.matchMedia( "only all" ).matches;
43
44 //if media queries are supported, exit here
45 if( respond.mediaQueriesSupported ){ return; }
46
47 //define vars
48 var doc = win.document,
49 docElem = doc.documentElement,
50 mediastyles = [],
51 rules = [],
52 appendedEls = [],
53 parsedSheets = {},
54 resizeThrottle = 30,
55 head = doc.getElementsByTagName( "head" )[0] || docElem,
56 base = doc.getElementsByTagName( "base" )[0],
57 links = head.getElementsByTagName( "link" ),
58 requestQueue = [],
59
60 //loop stylesheets, send text content to translate
61 ripCSS = function(){
62 var sheets = links,
63 sl = sheets.length,
64 i = 0,
65 //vars for loop:
66 sheet, href, media, isCSS;
67
68 for( ; i < sl; i++ ){
69 sheet = sheets[ i ],
70 href = sheet.href,
71 media = sheet.media,
72 isCSS = sheet.rel && sheet.rel.toLowerCase() === "stylesheet";
73
74 //only links plz and prevent re-parsing
75 if( !!href && isCSS && !parsedSheets[ href ] ){
76 // selectivizr exposes css through the rawCssText expando
77 if (sheet.styleSheet && sheet.styleSheet.rawCssText) {
78 translate( sheet.styleSheet.rawCssText, href, media );
79 parsedSheets[ href ] = true;
80 } else {
81 if( (!/^([a-zA-Z:]*\/\/)/.test( href ) && !base)
82 || href.replace( RegExp.$1, "" ).split( "/" )[0] === win.location.host ){
83 requestQueue.push( {
84 href: href,
85 media: media
86 } );
87 }
88 }
89 }
90 }
91 makeRequests();
92 },
93
94 //recurse through request queue, get css text
95 makeRequests = function(){
96 if( requestQueue.length ){
97 var thisRequest = requestQueue.shift();
98
99 ajax( thisRequest.href, function( styles ){
100 translate( styles, thisRequest.href, thisRequest.media );
101 parsedSheets[ thisRequest.href ] = true;
102 makeRequests();
103 } );
104 }
105 },
106
107 //find media blocks in css text, convert to style blocks
108 translate = function( styles, href, media ){
109 var qs = styles.match( /@media[^\{]+\{([^\{\}]*\{[^\}\{]*\})+/gi ),
110 ql = qs && qs.length || 0,
111 //try to get CSS path
112 href = href.substring( 0, href.lastIndexOf( "/" )),
113 repUrls = function( css ){
114 return css.replace( /(url\()['"]?([^\/\)'"][^:\)'"]+)['"]?(\))/g, "$1" + href + "$2$3" );
115 },
116 useMedia = !ql && media,
117 //vars used in loop
118 i = 0,
119 j, fullq, thisq, eachq, eql;
120
121 //if path exists, tack on trailing slash
122 if( href.length ){ href += "/"; }
123
124 //if no internal queries exist, but media attr does, use that
125 //note: this currently lacks support for situations where a media attr is specified on a link AND
126 //its associated stylesheet has internal CSS media queries.
127 //In those cases, the media attribute will currently be ignored.
128 if( useMedia ){
129 ql = 1;
130 }
131
132
133 for( ; i < ql; i++ ){
134 j = 0;
135
136 //media attr
137 if( useMedia ){
138 fullq = media;
139 rules.push( repUrls( styles ) );
140 }
141 //parse for styles
142 else{
143 fullq = qs[ i ].match( /@media *([^\{]+)\{([\S\s]+?)$/ ) && RegExp.$1;
144 rules.push( RegExp.$2 && repUrls( RegExp.$2 ) );
145 }
146
147 eachq = fullq.split( "," );
148 eql = eachq.length;
149
150 for( ; j < eql; j++ ){
151 thisq = eachq[ j ];
152 mediastyles.push( {
153 media : thisq.split( "(" )[ 0 ].match( /(only\s+)?([a-zA-Z]+)\s?/ ) && RegExp.$2 || "all",
154 rules : rules.length - 1,
155 hasquery: thisq.indexOf("(") > -1,
156 minw : thisq.match( /\(min\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/ ) && parseFloat( RegExp.$1 ) + ( RegExp.$2 || "" ),
157 maxw : thisq.match( /\(max\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/ ) && parseFloat( RegExp.$1 ) + ( RegExp.$2 || "" )
158 } );
159 }
160 }
161
162 applyMedia();
163 },
164
165 lastCall,
166
167 resizeDefer,
168
169 // returns the value of 1em in pixels
170 getEmValue = function() {
171 var ret,
172 div = doc.createElement('div'),
173 body = doc.body,
174 fakeUsed = false;
175
176 div.style.cssText = "position:absolute;font-size:1em;width:1em";
177
178 if( !body ){
179 body = fakeUsed = doc.createElement( "body" );
180 body.style.background = "none";
181 }
182
183 body.appendChild( div );
184
185 docElem.insertBefore( body, docElem.firstChild );
186
187 ret = div.offsetWidth;
188
189 if( fakeUsed ){
190 docElem.removeChild( body );
191 }
192 else {
193 body.removeChild( div );
194 }
195
196 //also update eminpx before returning
197 ret = eminpx = parseFloat(ret);
198
199 return ret;
200 },
201
202 //cached container for 1em value, populated the first time it's needed
203 eminpx,
204
205 //enable/disable styles
206 applyMedia = function( fromResize ){
207 var name = "clientWidth",
208 docElemProp = docElem[ name ],
209 currWidth = doc.compatMode === "CSS1Compat" && docElemProp || doc.body[ name ] || docElemProp,
210 styleBlocks = {},
211 lastLink = links[ links.length-1 ],
212 now = (new Date()).getTime();
213
214 //throttle resize calls
215 if( fromResize && lastCall && now - lastCall < resizeThrottle ){
216 clearTimeout( resizeDefer );
217 resizeDefer = setTimeout( applyMedia, resizeThrottle );
218 return;
219 }
220 else {
221 lastCall = now;
222 }
223
224 for( var i in mediastyles ){
225 var thisstyle = mediastyles[ i ],
226 min = thisstyle.minw,
227 max = thisstyle.maxw,
228 minnull = min === null,
229 maxnull = max === null,
230 em = "em";
231
232 if( !!min ){
233 min = parseFloat( min ) * ( min.indexOf( em ) > -1 ? ( eminpx || getEmValue() ) : 1 );
234 }
235 if( !!max ){
236 max = parseFloat( max ) * ( max.indexOf( em ) > -1 ? ( eminpx || getEmValue() ) : 1 );
237 }
238
239 // if there's no media query at all (the () part), or min or max is not null, and if either is present, they're true
240 if( !thisstyle.hasquery || ( !minnull || !maxnull ) && ( minnull || currWidth >= min ) && ( maxnull || currWidth <= max ) ){
241 if( !styleBlocks[ thisstyle.media ] ){
242 styleBlocks[ thisstyle.media ] = [];
243 }
244 styleBlocks[ thisstyle.media ].push( rules[ thisstyle.rules ] );
245 }
246 }
247
248 //remove any existing respond style element(s)
249 for( var i in appendedEls ){
250 if( appendedEls[ i ] && appendedEls[ i ].parentNode === head ){
251 head.removeChild( appendedEls[ i ] );
252 }
253 }
254
255 //inject active styles, grouped by media type
256 for( var i in styleBlocks ){
257 var ss = doc.createElement( "style" ),
258 css = styleBlocks[ i ].join( "\n" );
259
260 ss.type = "text/css";
261 ss.media = i;
262
263 //originally, ss was appended to a documentFragment and sheets were appended in bulk.
264 //this caused crashes in IE in a number of circumstances, such as when the HTML element had a bg image set, so appending beforehand seems best. Thanks to @dvelyk for the initial research on this one!
265 head.insertBefore( ss, lastLink.nextSibling );
266
267 if ( ss.styleSheet ){
268 ss.styleSheet.cssText = css;
269 }
270 else {
271 ss.appendChild( doc.createTextNode( css ) );
272 }
273
274 //push to appendedEls to track for later removal
275 appendedEls.push( ss );
276 }
277 },
278 //tweaked Ajax functions from Quirksmode
279 ajax = function( url, callback ) {
280 var req = xmlHttp();
281 if (!req){
282 return;
283 }
284 req.open( "GET", url, true );
285 req.onreadystatechange = function () {
286 if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){
287 return;
288 }
289 callback( req.responseText );
290 }
291 if ( req.readyState == 4 ){
292 return;
293 }
294 req.send( null );
295 },
296 //define ajax obj
297 xmlHttp = (function() {
298 var xmlhttpmethod = false;
299 try {
300 xmlhttpmethod = new XMLHttpRequest();
301 }
302 catch( e ){
303 xmlhttpmethod = new ActiveXObject( "Microsoft.XMLHTTP" );
304 }
305 return function(){
306 return xmlhttpmethod;
307 };
308 })();
309
310 //translate CSS
311 ripCSS();
312
313 //expose update for re-running respond later on
314 respond.update = ripCSS;
315
316 //adjust on resize
317 function callMedia(){
318 applyMedia( true );
319 }
320 if( win.addEventListener ){
321 win.addEventListener( "resize", callMedia, false );
322 }
323 else if( win.attachEvent ){
324 win.attachEvent( "onresize", callMedia );
325 }
326 })(this);
2 /*! NOTE: If you're already including a window.matchMedia polyfill via Modernizr or otherwise, you don't need this part */
3 window.matchMedia = window.matchMedia || (function(doc, undefined){
4
5 var bool,
6 docElem = doc.documentElement,
7 refNode = docElem.firstElementChild || docElem.firstChild,
8 // fakeBody required for <FF4 when executed in <head>
9 fakeBody = doc.createElement('body'),
10 div = doc.createElement('div');
11
12 div.id = 'mq-test-1';
13 div.style.cssText = "position:absolute;top:-100em";
14 fakeBody.style.background = "none";
15 fakeBody.appendChild(div);
16
17 return function(q){
18
19 div.innerHTML = '­<style media="'+q+'"> #mq-test-1 { width: 42px; }</style>';
20
21 docElem.insertBefore(fakeBody, refNode);
22 bool = div.offsetWidth == 42;
23 docElem.removeChild(fakeBody);
24
25 return { matches: bool, media: q };
26 };
27
28 })(document);
29
30
31
32
33 /*! Respond.js v1.2.0: min/max-width media query polyfill. (c) Scott Jehl. MIT/GPLv2 Lic. j.mp/respondjs */
34 (function( win ){
35 //exposed namespace
36 win.respond = {};
37
38 //define update even in native-mq-supporting browsers, to avoid errors
39 respond.update = function(){};
40
41 //expose media query support flag for external use
42 respond.mediaQueriesSupported = win.matchMedia && win.matchMedia( "only all" ).matches;
43
44 //if media queries are supported, exit here
45 if( respond.mediaQueriesSupported ){ return; }
46
47 //define vars
48 var doc = win.document,
49 docElem = doc.documentElement,
50 mediastyles = [],
51 rules = [],
52 appendedEls = [],
53 parsedSheets = {},
54 resizeThrottle = 30,
55 head = doc.getElementsByTagName( "head" )[0] || docElem,
56 base = doc.getElementsByTagName( "base" )[0],
57 links = head.getElementsByTagName( "link" ),
58 requestQueue = [],
59
60 //loop stylesheets, send text content to translate
61 ripCSS = function(){
62 var sheets = links,
63 sl = sheets.length,
64 i = 0,
65 //vars for loop:
66 sheet, href, media, isCSS;
67
68 for( ; i < sl; i++ ){
69 sheet = sheets[ i ],
70 href = sheet.href,
71 media = sheet.media,
72 isCSS = sheet.rel && sheet.rel.toLowerCase() === "stylesheet";
73
74 //only links plz and prevent re-parsing
75 if( !!href && isCSS && !parsedSheets[ href ] ){
76 // selectivizr exposes css through the rawCssText expando
77 if (sheet.styleSheet && sheet.styleSheet.rawCssText) {
78 translate( sheet.styleSheet.rawCssText, href, media );
79 parsedSheets[ href ] = true;
80 } else {
81 if( (!/^([a-zA-Z:]*\/\/)/.test( href ) && !base)
82 || href.replace( RegExp.$1, "" ).split( "/" )[0] === win.location.host ){
83 requestQueue.push( {
84 href: href,
85 media: media
86 } );
87 }
88 }
89 }
90 }
91 makeRequests();
92 },
93
94 //recurse through request queue, get css text
95 makeRequests = function(){
96 if( requestQueue.length ){
97 var thisRequest = requestQueue.shift();
98
99 ajax( thisRequest.href, function( styles ){
100 translate( styles, thisRequest.href, thisRequest.media );
101 parsedSheets[ thisRequest.href ] = true;
102 makeRequests();
103 } );
104 }
105 },
106
107 //find media blocks in css text, convert to style blocks
108 translate = function( styles, href, media ){
109 var qs = styles.match( /@media[^\{]+\{([^\{\}]*\{[^\}\{]*\})+/gi ),
110 ql = qs && qs.length || 0,
111 //try to get CSS path
112 href = href.substring( 0, href.lastIndexOf( "/" )),
113 repUrls = function( css ){
114 return css.replace( /(url\()['"]?([^\/\)'"][^:\)'"]+)['"]?(\))/g, "$1" + href + "$2$3" );
115 },
116 useMedia = !ql && media,
117 //vars used in loop
118 i = 0,
119 j, fullq, thisq, eachq, eql;
120
121 //if path exists, tack on trailing slash
122 if( href.length ){ href += "/"; }
123
124 //if no internal queries exist, but media attr does, use that
125 //note: this currently lacks support for situations where a media attr is specified on a link AND
126 //its associated stylesheet has internal CSS media queries.
127 //In those cases, the media attribute will currently be ignored.
128 if( useMedia ){
129 ql = 1;
130 }
131
132
133 for( ; i < ql; i++ ){
134 j = 0;
135
136 //media attr
137 if( useMedia ){
138 fullq = media;
139 rules.push( repUrls( styles ) );
140 }
141 //parse for styles
142 else{
143 fullq = qs[ i ].match( /@media *([^\{]+)\{([\S\s]+?)$/ ) && RegExp.$1;
144 rules.push( RegExp.$2 && repUrls( RegExp.$2 ) );
145 }
146
147 eachq = fullq.split( "," );
148 eql = eachq.length;
149
150 for( ; j < eql; j++ ){
151 thisq = eachq[ j ];
152 mediastyles.push( {
153 media : thisq.split( "(" )[ 0 ].match( /(only\s+)?([a-zA-Z]+)\s?/ ) && RegExp.$2 || "all",
154 rules : rules.length - 1,
155 hasquery: thisq.indexOf("(") > -1,
156 minw : thisq.match( /\(min\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/ ) && parseFloat( RegExp.$1 ) + ( RegExp.$2 || "" ),
157 maxw : thisq.match( /\(max\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/ ) && parseFloat( RegExp.$1 ) + ( RegExp.$2 || "" )
158 } );
159 }
160 }
161
162 applyMedia();
163 },
164
165 lastCall,
166
167 resizeDefer,
168
169 // returns the value of 1em in pixels
170 getEmValue = function() {
171 var ret,
172 div = doc.createElement('div'),
173 body = doc.body,
174 fakeUsed = false;
175
176 div.style.cssText = "position:absolute;font-size:1em;width:1em";
177
178 if( !body ){
179 body = fakeUsed = doc.createElement( "body" );
180 body.style.background = "none";
181 }
182
183 body.appendChild( div );
184
185 docElem.insertBefore( body, docElem.firstChild );
186
187 ret = div.offsetWidth;
188
189 if( fakeUsed ){
190 docElem.removeChild( body );
191 }
192 else {
193 body.removeChild( div );
194 }
195
196 //also update eminpx before returning
197 ret = eminpx = parseFloat(ret);
198
199 return ret;
200 },
201
202 //cached container for 1em value, populated the first time it's needed
203 eminpx,
204
205 //enable/disable styles
206 applyMedia = function( fromResize ){
207 var name = "clientWidth",
208 docElemProp = docElem[ name ],
209 currWidth = doc.compatMode === "CSS1Compat" && docElemProp || doc.body[ name ] || docElemProp,
210 styleBlocks = {},
211 lastLink = links[ links.length-1 ],
212 now = (new Date()).getTime();
213
214 //throttle resize calls
215 if( fromResize && lastCall && now - lastCall < resizeThrottle ){
216 clearTimeout( resizeDefer );
217 resizeDefer = setTimeout( applyMedia, resizeThrottle );
218 return;
219 }
220 else {
221 lastCall = now;
222 }
223
224 for( var i in mediastyles ){
225 var thisstyle = mediastyles[ i ],
226 min = thisstyle.minw,
227 max = thisstyle.maxw,
228 minnull = min === null,
229 maxnull = max === null,
230 em = "em";
231
232 if( !!min ){
233 min = parseFloat( min ) * ( min.indexOf( em ) > -1 ? ( eminpx || getEmValue() ) : 1 );
234 }
235 if( !!max ){
236 max = parseFloat( max ) * ( max.indexOf( em ) > -1 ? ( eminpx || getEmValue() ) : 1 );
237 }
238
239 // if there's no media query at all (the () part), or min or max is not null, and if either is present, they're true
240 if( !thisstyle.hasquery || ( !minnull || !maxnull ) && ( minnull || currWidth >= min ) && ( maxnull || currWidth <= max ) ){
241 if( !styleBlocks[ thisstyle.media ] ){
242 styleBlocks[ thisstyle.media ] = [];
243 }
244 styleBlocks[ thisstyle.media ].push( rules[ thisstyle.rules ] );
245 }
246 }
247
248 //remove any existing respond style element(s)
249 for( var i in appendedEls ){
250 if( appendedEls[ i ] && appendedEls[ i ].parentNode === head ){
251 head.removeChild( appendedEls[ i ] );
252 }
253 }
254
255 //inject active styles, grouped by media type
256 for( var i in styleBlocks ){
257 var ss = doc.createElement( "style" ),
258 css = styleBlocks[ i ].join( "\n" );
259
260 ss.type = "text/css";
261 ss.media = i;
262
263 //originally, ss was appended to a documentFragment and sheets were appended in bulk.
264 //this caused crashes in IE in a number of circumstances, such as when the HTML element had a bg image set, so appending beforehand seems best. Thanks to @dvelyk for the initial research on this one!
265 head.insertBefore( ss, lastLink.nextSibling );
266
267 if ( ss.styleSheet ){
268 ss.styleSheet.cssText = css;
269 }
270 else {
271 ss.appendChild( doc.createTextNode( css ) );
272 }
273
274 //push to appendedEls to track for later removal
275 appendedEls.push( ss );
276 }
277 },
278 //tweaked Ajax functions from Quirksmode
279 ajax = function( url, callback ) {
280 var req = xmlHttp();
281 if (!req){
282 return;
283 }
284 req.open( "GET", url, true );
285 req.onreadystatechange = function () {
286 if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){
287 return;
288 }
289 callback( req.responseText );
290 }
291 if ( req.readyState == 4 ){
292 return;
293 }
294 req.send( null );
295 },
296 //define ajax obj
297 xmlHttp = (function() {
298 var xmlhttpmethod = false;
299 try {
300 xmlhttpmethod = new XMLHttpRequest();
301 }
302 catch( e ){
303 xmlhttpmethod = new ActiveXObject( "Microsoft.XMLHTTP" );
304 }
305 return function(){
306 return xmlhttpmethod;
307 };
308 })();
309
310 //translate CSS
311 ripCSS();
312
313 //expose update for re-running respond later on
314 respond.update = ripCSS;
315
316 //adjust on resize
317 function callMedia(){
318 applyMedia( true );
319 }
320 if( win.addEventListener ){
321 win.addEventListener( "resize", callMedia, false );
322 }
323 else if( win.attachEvent ){
324 win.attachEvent( "onresize", callMedia );
325 }
326 })(this);